define(["jquery", "bootstrap", "backend", "table", "form", "jstree"], function (
  $,
  undefined,
  Backend,
  Table,
  Form,
  undefined
) {
  var Controller = {
    index: function () {
      // tab栏切换事件 => 绑定事件
      $('a[data-toggle="tab"]').on("shown.bs.tab", function () {
        // 通过a标签里的href属性获取相应的id
        var panel = $($(this).attr("href"));

        if (panel.length > 0) {
          Controller.table[panel.attr("id")].call(this);

          // 切换tab模拟点击刷新按钮
          $(this).on("click", function (e) {
            $($(this).attr("href")).find(".btn-refresh").trigger("click"); //trigger触发器
          });
        }
        // 移除绑定的事件
        $(this).unbind("shown.bs.tab");
      });
      // 必须默认触发shown.bs.tab事件
      $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");
    },
    table: {
      subject: function () {
        // 初始化表格参数配置
        Table.api.init({
          extend: {
            index_url: "subject/subject/recyclebin",
            del_url: "subject/subject/destroy",
            restore_url: "subject/subject/restore",
            table: "subject",
          },
        });

        //获取table元素
        var SubjectTable = $("#SubjectTable");

        // 初始化表格
        SubjectTable.bootstrapTable({
          url: $.fn.bootstrapTable.defaults.extend.index_url,
          toolbar: "SubjectToolbar",
          pk: "id",
          sortName: "deletetime",
          sortOrder: "desc",
          extend: {
            index_url: "subject/subject/recyclebin",
            del_url: "subject/subject/destroy",
            restore_url: "subject/subject/restore",
            table: "subject",
          },
          columns: [
            [
              { checkbox: true }, //复选框
              { field: "id", title: __("subId") }, //对应复选框的内容:id
              { field: "title", title: __("Title"), operate: "LIKE" }, //operate表示可搜索键
              //课程图片
              {
                field: "thumb_cdn",
                title: __("ThumbCdn"),
                operate: false,
                events: Table.api.events.image,
                formatter: Table.api.formatter.image,
              },
              //点赞数
              {
                field: "likes",
                title: __("Likes"),
                operate: false,
              },
              //课程价格
              {
                field: "price",
                title: __("Price"),
                operate: false,
              },
              //创建时间
              {
                field: "deletetime",
                title: __("DeleteTime"),
                operate: false,
                formatter: Table.api.formatter.datetime,
                operate: "RANGE",
                addclass: "datetimerange",
                sortable: true,
              },
              //课程分类选项
              {
                field: "category.name",
                title: __("CategoryName"),
                operate: "LIKE",
              },
              //课程老师选项
              {
                field: "teacher.name",
                title: __("TeacherName"),
                operate: "LIKE",
              },
              //操作选项
              {
                field: "operate",
                title: __("Operate"),
                table: SubjectTable,
                events: Table.api.events.operate,
                formatter: Table.api.formatter.operate,
                buttons: [
                  {
                    name: "restore",
                    title: "恢复",
                    icon: "fa fa-reply",
                    classname: "btn btn-xs btn-success btn-magic btn-ajax",
                    url: $.fn.bootstrapTable.defaults.extend.restore_url,
                    extend: 'data-toggle="tooltip" data-container="body"',
                    confirm: "是否确认还原该课程",
                    success: () => SubjectTable.bootstrapTable("refresh"),
                  },
                ],
              },
            ],
          ],
        });

        // 工具栏的还原按钮
        $(".btn-restore").on("click", function () {
          let ids = Table.api.selectedids(SubjectTable);

          Layer.confirm(
            "是否确认还原选中的课程？",
            { icon: 3, title: "还原" },
            function (index) {
              Fast.api.ajax(
                {
                  url: $.fn.bootstrapTable.defaults.extend.restore_url,
                  data: {
                    ids,
                  },
                },
                function (res) {
                  // 关闭确认窗口
                  Layer.close(index);

                  // 刷新表格
                  SubjectTable.bootstrapTable("refresh");
                }
              );
            }
          );
        });

        // 工具栏的删除按钮
        $(".btn-multi").on("click", function () {
          let ids = Table.api.selectedids(SubjectTable);

          Layer.confirm(
            "是否确认删除选中的课程？",
            { icon: 3, title: "删除" },
            function (index) {
              Fast.api.ajax(
                {
                  url: $.fn.bootstrapTable.defaults.extend.del_url,
                  data: {
                    ids,
                  },
                },
                function (res) {
                  // 关闭确认窗口
                  Layer.close(index);

                  // 刷新表格
                  SubjectTable.bootstrapTable("refresh");
                }
              );
            }
          );
        });

        // 为表格绑定事件
        Table.api.bindevent(SubjectTable);
      },
      order: function () {
        // 初始化表格参数配置
        Table.api.init({
          extend: {
            index_url: "subject/order/recyclebin",
            del_url: "subject/order/destroy",
            restore_url: "subject/order/restore",
            table: "subject_order",
          },
        });

        //获取table元素
        var OrderTable = $("#OrderTable");

        // 初始化表格
        OrderTable.bootstrapTable({
          url: $.fn.bootstrapTable.defaults.extend.index_url,
          toolbar: "OrderToolbar",
          pk: "id",
          sortName: "deletetime",
          sortOrder: "desc",
          columns: [
            [
              { checkbox: true },
              { field: "id", title: __("Orderid") },
              { field: "subid", title: __("Subid") },
              { field: "busid", title: __("Busid") },
              { field: "total", title: __("Total"), operate: "BETWEEN" },
              {
                field: "code",
                title: __("Code"),
                operate: "LIKE",
                table: OrderToolbar,
                class: "autocontent",
                formatter: Table.api.formatter.content,
              },
              {
                field: "deletetime",
                title: __("DeleteTime"),
                operate: "RANGE",
                addclass: "datetimerange",
                autocomplete: false,
                formatter: Table.api.formatter.datetime,
              },
              { field: "couponid", title: __("Couponid") },
              {
                field: "pay",
                title: __("Pay"),
                searchList: {
                  money: __("Money"),
                  wx: __("Wx"),
                  zfb: __("Zfb"),
                },
                formatter: Table.api.formatter.normal,
              },
              {
                field: "operate",
                title: __("Operate"),
                table: OrderTable,
                events: Table.api.events.operate,
                formatter: Table.api.formatter.operate,
                buttons: [
                  {
                    name: "restore",
                    title: "恢复",
                    icon: "fa fa-reply",
                    classname: "btn btn-xs btn-success btn-magic btn-ajax",
                    url: $.fn.bootstrapTable.defaults.extend.restore_url,
                    extend: 'data-toggle="tooltip" data-container="body"',
                    confirm: "是否确认还原该课程",
                    success: () => OrderTable.bootstrapTable("refresh"),
                  },
                ],
              },
            ],
          ],
        });

        // 工具栏的还原按钮
        $(".btn-restore").on("click", function () {
          let ids = Table.api.selectedids(OrderTable);

          Layer.confirm(
            "是否确认还原选中的订单？",
            { icon: 3, title: "还原" },
            function (index) {
              Fast.api.ajax(
                {
                  url: $.fn.bootstrapTable.defaults.extend.restore_url,
                  data: {
                    ids,
                  },
                },
                function (res) {
                  // 关闭确认窗口
                  Layer.close(index);

                  // 刷新表格
                  OrderTable.bootstrapTable("refresh");
                }
              );
            }
          );
        });
        // 为表格绑定事件
        Table.api.bindevent(OrderTable);
      },
    },
    api: {
      bindevent: function () {
        Form.api.bindevent($("form[role=form]"));
      },
    },
  };

  return Controller;
});
